package com.cmc.cloud.cmclink.doc.controller.admin.multimodaltransport;

import com.cmc.cloud.cmclink.doc.api.document.req.cargomanifest.CargoManifestReqDTO;
import com.cmc.cloud.cmclink.doc.constants.UrlConstants;
import com.cmc.cloud.cmclink.doc.constants.errorconstant.BargeMultiCalculationErrorCodeConstants;
import com.cmc.cloud.cmclink.doc.service.manifest.CargoManifestService;
import com.cmc.cloud.cmclink.doc.service.multimodaltransport.TransferAgentOperationService;
import com.cmc.cloud.cmclink.doc.vo.multimodaltransport.multiop.*;
import com.cmes.framework.common.exception.util.ServiceExceptionUtil;
import com.cmes.framework.common.pojo.CommonResult;
import com.cmes.framework.common.pojo.PageResult;
import com.cmes.framework.operatelog.core.annotations.OperateLog;
import com.cmes.framework.operatelog.core.enums.OperateTypeEnum;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.ObjectUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 * 多式联运-中转代理操作Controller
 *
 * @author codegen
 */
@Tag(name = "管理后台 - 多式联运中转代理操作")
@RestController
@Validated
@RequestMapping(UrlConstants.APPLICATION_URL + "/transferAgentOperation")
public class TransferAgentOperationController {

    @Resource
    private TransferAgentOperationService transferAgentOperationService;

    @Resource
    private CargoManifestService cargoManifestService;

    @Operation(summary = "进口-中转代理列表查询")
    @OperateLog(type = OperateTypeEnum.GET)
    @PostMapping("/queryTransferAgent")
    @PreAuthorize("@ss.hasPermission('doc:transitProxy:search')")
    public CommonResult<PageResult<MultiOpTransferAgentPageRespVO>> queryTransferAgent(@Valid @RequestBody MultiOpTransferAgentPageReqVO req) {
        return CommonResult.success(transferAgentOperationService.queryTransferAgent(req));
    }


    @Operation(summary = "进口-set D/O")
    @OperateLog(type = OperateTypeEnum.UPDATE)
    @PostMapping("/setDo")
    @PreAuthorize("@ss.hasPermission('doc:transitProxy:setDo')")
    public CommonResult<Void> setDo(@Valid @RequestBody MultiOpTransferAgentSetDoVO multiOpTransferAgentSetDoVo) {
        transferAgentOperationService.setDo(multiOpTransferAgentSetDoVo);
        return CommonResult.success();
    }

    @Operation(summary = "进口-下载Cargo Manifest")
    @OperateLog(type = OperateTypeEnum.EXPORT)
    @PostMapping("/downloadManifest")
    @PreAuthorize("@ss.hasPermission('doc:transitProxy:manifestDownLoad')")
    public CommonResult<Void> downloadManifest(HttpServletResponse response,
                                               @Valid @RequestBody CargoManifestReqDTO reqDto) {
        if (ObjectUtils.isEmpty(reqDto.getBlNoIds())){
            throw ServiceExceptionUtil.exception(BargeMultiCalculationErrorCodeConstants.BLNO_ID_LIST_IS_EMPTY);
        }
        cargoManifestService.downloadCargoManifest(response, reqDto);
        return CommonResult.success();
    }

}
